/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wisnu.tutorial.configuration;

/**
 *
 * @author Wisnu Wardoyo
 */
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

public class ServerConfiguration {

    static final String CONFIG = "config";
    static final String IP = "ip";
    static final String DB = "db";
    static final String USER = "user";
    static final String PASS = "pass";

    @SuppressWarnings({"unchecked", "null", "empty-statement"})
    public List<String> getServerConfiguration(String configFile) {
        List<String> configData = new ArrayList<>();;
        try {
            // First, create a new XMLInputFactory
            XMLInputFactory inputFactory = XMLInputFactory.newInstance();
            // Setup a new eventReader
            InputStream in = new FileInputStream(configFile);
            XMLEventReader eventReader = inputFactory.createXMLEventReader(in);

            while (eventReader.hasNext()) {
                XMLEvent event = eventReader.nextEvent();

                if (event.isStartElement()) {
                    StartElement startElement = event.asStartElement();
                    // If we have an item element, we create a new item
                    if (startElement.getName().getLocalPart().equals(CONFIG)) {

                    }

                    if (event.isStartElement()) {
                        if (event.asStartElement().getName().getLocalPart()
                                .equals(IP)) {
                            event = eventReader.nextEvent();
                            configData.add(event.asCharacters().getData());
                            continue;
                        }
                    }
                    if (event.asStartElement().getName().getLocalPart()
                            .equals(DB)) {
                        event = eventReader.nextEvent();
                        configData.add(event.asCharacters().getData());
                        continue;
                    }
                    if (event.asStartElement().getName().getLocalPart()
                            .equals(USER)) {
                        event = eventReader.nextEvent();
                        configData.add(event.asCharacters().getData());
                        continue;
                    }

                    if (event.asStartElement().getName().getLocalPart()
                            .equals(PASS)) {
                        event = eventReader.nextEvent();
                        configData.add(event.asCharacters().getData().equals("?") ? "" : event.asCharacters().getData());
                        continue;
                    }

                }
                // If we reach the end of an item element, we add it to the list
                if (event.isEndElement()) {
                    EndElement endElement = event.asEndElement();
                    if (endElement.getName().getLocalPart().equals(CONFIG)) {

                    }
                }

            }
        } catch (FileNotFoundException | XMLStreamException e) {
            System.err.println(e);
        }
        return configData;
    }
}
